<?php
/**
 * ListCopyProducts.php class file.
 * @author wudg <544038230@qq.com>
 * @date 2021/1/14- 下午2:55
 */

namespace common\models\es\dataProvider\copyProducts;
use Yii;

class ListCopyProducts extends Query
{
    public function getData()
    {
        $params = [
            'index' => $this->getIndexName(),
            'body' => $this->getBody()
        ];
//        print_r($this->query);
//        print_r(json_encode($params['body'])); exit;
        $esResult = Yii::$app->elasticsearch->search($params);
        $count = $esResult['hits']['total']['value'] ?? 0;
        $list = $esResult['hits']['hits'] ?? [];
        return [$list, $count];
    }


    /**
     * 拼装请求体
     * @return array
     */
    protected function getBody()
    {
        $return = [
            'track_total_hits' => true,
            '_source' => $this->fields,
            'size' => $this->size,
            'from' => $this->from,
            'query' => $this->getQueryBody(),
            'sort' => $this->sort
        ];
        if (isset($this->query['title']) && $this->query['title']) {
            $return['highlight'] = ['fields' => ['title' => (object)[]]];
        }
        return $return;
    }

    protected function getQueryBody()
    {
        return [
            'bool' => [
                'filter' => $this->getFilter()
            ],
        ];
    }

    /**
     * 拼装过滤条件
     * @return array
     */
    protected function getFilter(): array
    {

        $filter = [];
        //用户ID
        if (isset($this->query['uid']) && !empty($this->query['uid'])) {
            $filter[] =
                ['term' => ['uid' => $this->query['uid']]]
            ;
        }

        //店铺id
        if (isset($this->query['mall_id']) && !empty($this->query['mall_id'])) {
            $filter[] =
                ['term' => ['mall_id' => $this->query['mall_id']]]
            ;
        }

        //是否删除
        if(isset($this->query['is_delete']))
        {
            $filter[] =
                ['term' => ['is_delete' => $this->query['is_delete']]]
            ;
        }

        //复制状态
        if(isset($this->query['status']))
        {
            $filter[] =
                ['terms' => ['status' => $this->query['status']]]
            ;
        }

        /**
         * 宝贝状态
         */
        if(isset($this->query['product_status']))
        {
            $filter[] =
                ['term' => ['product_status' => $this->query['product_status']]]
            ;
        }

        //批次ID
        if(isset($this->query['batch_no']) && $this->query['batch_no'] != null)
        {
            $filter[] =
                ['term' => ['batch_no' => $this->query['batch_no']]]
            ;
        }
        //货源商品ID,支持多个批量检索
        if(isset($this->query['num_iid']) && is_array($this->query['num_iid']))
        {
            $filter[] =
                ['terms' => ['num_iid' => $this->query['num_iid']]]
            ;
        }

        //平台ID
        if(isset($this->query['platform']) && $this->query['platform'] != null)
        {
            $filter[] =
                ['term' => ['platform' => $this->query['platform']]]
            ;
        }

        //货源去向
        if(isset($this->query['to_type']) && $this->query['to_type'] != null)
        {
            $filter[] =
                ['term' => ['to_type' => $this->query['to_type']]]
            ;
        }
        //是否搬家
        if (isset($this->query['is_copy']) && $this->query['is_copy'] != null)
        {
            $filter[] =
                ['term' => ['is_copy' => $this->query['is_copy']]]
            ;
        }

        //根据商品名称搜索关键词
        if (isset($this->query['title']) && !empty($this->query['title'])) {
            $filter[] =
                ['match' => ['title' => $this->query['title']]]
            ;
        }


        /**
         * 入库时间区间检索
         */
        if (isset($this->query['start_at']) || isset($this->query['end_at'])) {
            $timeTep = [];
            if (isset($this->query['start_at']) && !empty($this->query['start_at'])) {
                $timeTep['from'] = $this->query['start_at'];
            }
            if (isset($this->query['end_at']) && !empty($this->query['end_at'])) {
                //截止时间加上一天的时间戳
                $timeTep['to'] = $this->query['end_at'];
            }
            if (count($timeTep)) {
                $filter[] = [
                    "range" => [
                        'created_at' => $timeTep
                    ]
                ];
                unset($timeTep);
            }
        }
        return $filter;
    }

}